package com.ja.openchat.ui.found;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.google.zxing.integration.android.IntentIntegrator;
import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.databinding.FragmentFoundBinding;
import com.ja.openchat.ui.message.ScanActivity;
import com.ja.openchat.viewmodels.FoundViewModel;
import com.ja.openchat.views.QBadgeView;
import com.ja.openchat.views.SpaceItemDecoration;
import com.lxj.xpopup.util.XPopupUtils;
import com.zy.openchat.core.model.config.AppConfigInfo;

import me.goldze.mvvmhabit.base.BaseFragment;
import q.rorbin.badgeview.Badge;

public class FoundFragment extends BaseFragment<FragmentFoundBinding, FoundViewModel> {

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_found;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initView() {
        super.initView();
//        binding.recyclerView.addItemDecoration(new SpaceItemDecoration(XPopupUtils.dp2px(getContext(),10),false,SpaceItemDecoration.LINEARLAYOUT));

        binding.tvScan.setOnClickListener(v -> {
            IntentIntegrator integrator = new IntentIntegrator((Activity) getActivity());
            integrator.setPrompt("");
            integrator.setBeepEnabled(false);
            integrator.setBarcodeImageEnabled(false);
            integrator.setOrientationLocked(false);
            integrator.setCaptureActivity(ScanActivity.class);
            integrator.initiateScan();
        });
//        View chatCircle = getLayoutInflater().inflate(R.layout.item_multi_found_top,binding.recyclerView,false);

        AppConfigInfo configInfo = AppConfigInfo.getAppWithoutLoginConfigInfo();
        if (configInfo != null && configInfo.can_see_emoji_shop) {
            binding.tvEmojiShop.setVisibility(View.VISIBLE);
        } else {
            binding.tvEmojiShop.setVisibility(View.GONE);
        }

        if (configInfo != null && !configInfo.can_see_blog) {
            binding.tvGoChatCircle.setVisibility(View.GONE);

        } else {
            QBadgeView qBadgeView = new QBadgeView(getContext());
            qBadgeView.setBadgeTextSize(9, true);
            qBadgeView.setGravityOffset(10, 0, true);
            qBadgeView.bindTarget(binding.tvGoChatCircle).setBadgePadding(6, true)
                    .setShowShadow(false)
                    .setBadgeGravity(Gravity.CENTER | Gravity.END)
                    .setOnDragStateChangedListener(new Badge.OnDragStateChangedListener() {
                        @Override
                        public void onDragStateChanged(int dragState, Badge badge, View targetView) {
                        }
                    });
            viewModel.mCircleUnreadCount.observe(this, new Observer<Integer>() {
                @Override
                public void onChanged(Integer integer) {
                    if (integer != null)
                        qBadgeView.setBadgeNumber(integer);
                }
            });
        }
    }
}